<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>

<canvas id="canvas" class="output" width="300" height="350">
    <button id="button1"></button>
    <button id="button2"></button>
</canvas>

<script>
test(function(t) {
    document.getElementById("button1").focus();
    var canvas = document.getElementById("canvas").getContext("2d");

    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(undefined);
    });
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(undefined, undefined);
    });
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(null);
    });
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(null, null);
    });
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded();
    });

    var path = new Path2D();
    path.rect(50, 50, 200, 100);
    canvas.fillStyle = "#ccf";
    canvas.fill(path);
    // re-test null case after having defined a path (regression test for crbug.com/353248)
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(null);
    });
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(null, null);
    });
    canvas.drawFocusIfNeeded(path, document.getElementById("button1"));

    path = new Path2D();
    path.rect(50, 200, 200, 100);
    canvas.fillStyle = "#cfc";
    canvas.fill(path);
    canvas.drawFocusIfNeeded(path, document.getElementById("button2"));

    // The top rect"s focus ring is tied to button1, which is focused.
    // It should have an outline in some color other than the background color.
    var imageData = canvas.getImageData(49, 50, 1, 1);
    var data = imageData.data;
    assert_true(data[0] != 0 || data[1] != 0 || data[2] != 0);

    // The bottom rect"s focus ring is tied to button2, which is not focused.
    imageData = canvas.getImageData(49, 200, 1, 1);
    data = imageData.data;
    assert_true(data[0] == 0 && data[1] == 0 && data[2] == 0);
}, 'Canvas test: drawFocusIfNeeded with Path2D');
</script>
</body>
